<template>
  <div class="article-page">
    <van-nav-bar fixed placeholder>
      <template #left>
        <van-tabs
          color="#000"
          title-inactive-color="#aaa"
          line-width="12px"
          v-model="active"
        >
          <van-tab title="推荐"></van-tab>
          <van-tab title="最新"></van-tab>
        </van-tabs>
      </template>
      <template #right>
        <div class="logo">
          <img
            src="http://luckybo0027.gitee.io/hm-vant-h5/img/logo.dbd40807.png"
            alt=""
          />
        </div>
      </template>
    </van-nav-bar>
    <van-list
      v-model="loading"
      :finished="current > pageTotal"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <article-list v-for="item in list" :key="item.id" :items="item" />
    </van-list>
  </div>
</template>

<script>
import ArticleList from "@/components/article-list.vue";
import { articleList_API } from "@/api/article.js";
export default {
  name: "article-item",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      current: 1,
      pageTotal: 0,
      active: 0,
    };
  },
  components: {
    ArticleList,
  },
  created() {
    this.onLoad();
  },
  methods: {
    async onLoad() {
      const result = await articleList_API({
        current: this.current,
        sorter: this.active ? "" : "weight_desc",
      });
      this.current++;
      this.list = this.list.concat(result.data.data.rows);
      this.pageTotal = result.data.data.pageTotal;
      this.loading = false;
    },
  },
  watch: {
    active() {
      this.list = [];
      this.current = 1;
      this.pageTotal = 0;
      this.onLoad();
    },
  },
};
</script>

<style lang="less" scoped>
.article-page {
  .logo {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    > img {
      width: 64px;
      height: 28px;
      display: block;
      margin-right: 10px;
    }
  }
}
</style>